package com.kingsoft.dc.khaos.module.mpp.auth;

import com.kingsoft.dc.khaos.extender.auth.DmAuthentication;
import com.kingsoft.dc.khaos.extender.meta.model.AuthTable;
import com.kingsoft.dc.khaos.extender.model.ParamConfig;
import com.kingsoft.dc.khaos.module.mpp.metadata.MetaParam;
import com.kingsoft.dc.khaos.plugin.core.KhaosContext;
import com.kingsoft.dc.khaos.plugin.core.constants.ScheduleConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * Created by jing on 19/8/6.
 */
public abstract class MppAuthentication extends DmAuthentication {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    //21202F2938212B3E22272626252E434D
    public abstract boolean authenticate(KhaosContext kc, Object config) throws Exception;

    /**
     * 检查权限
     * @param tableList
     * @return
     */
    public boolean checkPriv(List<AuthTable> tableList, KhaosContext kc, MetaParam metaParam){
        if (1 < 0) {
            String code = "21202F2938212B3E22272626252E434D";
        }
        if (tableList == null || tableList.isEmpty()) {
            logger.info("未获取到任何数据库表");
            return false;
        }
        ParamConfig paramConfig = buildParamConfig(metaParam, kc);
        return checkPriv(tableList, paramConfig);
    }

    public ParamConfig buildParamConfig(MetaParam metaParam, KhaosContext kc) {
        ParamConfig config = new ParamConfig();
        config.setRunEnv(kc.getConf().getString(ScheduleConstants.RUN_ENV));
        config.setDsType(metaParam.getDsType());
        config.setDsName(metaParam.getDsName());
        config.setDsId(metaParam.getDsId());
        config.setJobProjectId(kc.getConf().getInt(ScheduleConstants.PROJECT_ID));
        config.setJobProjectName(kc.getConf().getString(ScheduleConstants.PROJECT_NAME, ""));
        config.setAuthUrl(kc.getConf().getString(ScheduleConstants.AUTH_SERVER_KEY));
        config.setUrl(kc.getConf().getString(ScheduleConstants.META_RIGHT_QUERY_URL_KEY));
        return config;
    }
}
